<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-11-11 15:35:21
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\Pro;


use App\Http\Services\Service;
use App\Repositories\ProProcessesWipRepository;
use Illuminate\Support\Facades\DB;

/** 
 * @property ProProcessesWipRepository $repository 
 * @author sunkaiyuan
 */
class WipService extends Service
    {
    public function __construct(ProProcessesWipRepository $repository)
        {
        $this->repository = $repository;
        }

    public function list()
        {
        // DB::enableQueryLog();
        $data = $this->repository->condition(request())->getGrid();
        // dd(DB::getQueryLog());
        return $data;
        }

    public function repairwip()
        {
        foreach (request('filter') as $key => $value) {
            foreach ($value as $key => $v)
                if ($key == 'sys_mpc_lot_defaultname') {
                    $this->repository->setQureySearch("sys_pro_process_wip.lot_name", $v['value'], $v['type'], $this->repository);
                    }
                elseif ($key == 'sys_mpc_location_name') {
                    }
                elseif ($key == 'available_quantity') {
                    $this->repository->setQureySearch("sys_pro_process_wip.faultle_quantity", $v['value'], $v['type'], $this->repository);
                    }
                elseif ($key == 'remark') {
                    $this->repository->setQureySearch("sys_pro_process_wip.dyelot", $v['value'], $v['type'], $this->repository);
                    }
                else
                    $this->repository->where("sys_pro_process_wip.{$key}", $v);
            }

        // DB::enableQueryLog();
        $data = $this->repository
            ->setRelation([])
            ->select(
                DB::raw("sys_pro_process_wip.id"),
                DB::raw("sys_pro_process_wip.dyelot as remark"),
                DB::raw("sys_pro_process_wip.lot_name as sys_mpc_lot_defaultname"),
                DB::raw("ROUND(sys_pro_process_wip.feeding_ratio*sys_pro_process_wip.faultle_quantity/1000,2) as sys_mpc_location_name"),
                DB::raw("sys_pro_process_wip.faultle_quantity AS available_quantity"),
                DB::raw("1 AS isrepair")
            )->where("sys_pro_process_wip.process_id", "=", "14")->getGrid();
        // dd(DB::getQueryLog());
        return $data;
        }
    }